package com.scaffold.chapter.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.chapter.entity.Chapter;
import com.scaffold.chapter.service.ChapterService;
import com.scaffold.commons.file.FileIO;
import com.scaffold.commons.json.JacksonUtil;
import com.scaffold.commons.web.CacheSite;

@Controller
public class ChapterAction extends BasicBizAction{
	@Autowired
	private ChapterService chapterService;
	@Autowired
	private FileIO fileIO;
	
	@RequestMapping("/chapter/view.xhtml")
	public String view(ModelMap model, Long id){
		if(id!=null){
			Chapter chapter = this.chapterService.getChapter(id);
			this.checkOwner(chapter);
			model.put("chapter", chapter);
		}
		return "/chapter/view.vm"; 
	}

	@RequestMapping("/chapter/backupImg.xhtml")
	public String backupImg(Long id) throws Exception{
		Chapter chapter = this.chapterService.getChapter(id);
		CacheSite cs = new CacheSite();
		cs.setCurrentUrl(chapter.getHref());
		cs.setHtmlContent(chapter.getHtml());
		cs.setFileIO(fileIO);
		cs.setStoreDir("/imgbackup");
		cs.store();
		chapter.setHtml(cs.getNewHtml());
		String imgMap = new JacksonUtil().objToJson(cs.getImgList());
		chapter.setImgMap(imgMap);
		this.chapterService.save(chapter);
		return this.getSuccessResult();
	}
	
	@RequestMapping("/chapter/pick.xhtml")
	public String pick(ModelMap model, Chapter chapter){
		model.put("chapter", chapter);
		return "/chapter/pick.vm"; 
	}

	@RequestMapping("/chapter/list.xhtml")
	public String list(ModelMap model, Long catalogId, Long topicId, String keyword){
		Page page = this.getPage(20);
		List<Chapter> list = chapterService.pageQuery(page, topicId, catalogId, keyword);
		model.put("chapterList", list);
		model.put("page", page);
		return "/chapter/list.vm";
	}
	
	@RequestMapping("/chapter/edit.xhtml")
	public String edit(ModelMap model, Long id){
		if(id!=null){
			Chapter chapter = this.chapterService.getChapter(id);
			this.checkOwner(chapter);
			model.put("chapter", chapter);	
		}
		return "/chapter/edit.vm"; 
	}
	
	@RequestMapping("/chapter/save.xhtml")
	public String save(Long id){
		if(id==null){
			Chapter chapter = new Chapter();
			this.bindParams(chapter);
			this.setOwner(chapter);
			this.chapterService.save(chapter);
		}else{
			Chapter chapter = this.chapterService.getChapter(id);
			this.checkOwner(chapter);
			this.bindParams(chapter);
			this.chapterService.update(chapter);
		}
		return "redirect:/chapter/list.xhtml";
	}
	
	@RequestMapping("/chapter/delete.xhtml")
	public String delete(Long id){
		Chapter chapter = this.chapterService.getChapter(id);
		this.checkOwner(chapter);
		this.chapterService.delete(chapter);
		return "redirect:/chapter/list.xhtml";
	}

	@RequestMapping("/chapter/test.xhtml")
	public String test(){
		return "/chapter/edit.vm";
	}

}
